package com.ziyia.databasetest;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

//public class DatabaseProvider extends ContentProvider {
//    private static final int bookDir = 0;
//    private static final int bookItem = 1;
//    private static final int categoryDir = 2;
//    private static final int categoryItem = 3;
//    private static final String AUTHORITY = "com.ziyia.databasetest.provider";
//    private MyDatabaseHelper myDatabaseHelper = null;
//    private static UriMatcher uriMatcher = null;
//
//
//    static {
//        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
//        uriMatcher.addURI(AUTHORITY, "book", bookDir);
//        uriMatcher.addURI(AUTHORITY, "book/#", bookItem);
//        uriMatcher.addURI(AUTHORITY, "category", categoryDir);
//        uriMatcher.addURI(AUTHORITY, "category/#", categoryItem);
//
//    }
//    public DatabaseProvider() {
//
//    }
//
//    @Override
//    public int delete(Uri uri, String selection, String[] selectionArgs) {
//        SQLiteDatabase db = myDatabaseHelper.getWritableDatabase();
//        int deleteRows = 0;
//        switch (uriMatcher.match(uri)) {
//            case bookDir:
//                deleteRows = db.delete("Book", selection, selectionArgs);
//                break;
//            case bookItem:
//                String bookId = uri.getPathSegments().get(1);
//                deleteRows = db.delete("Book", "id = ?", new String[]{bookId});
//                break;
//            case categoryDir:
//                deleteRows = db.delete("Category", selection, selectionArgs);
//                break;
//            case categoryItem:
//                String categoryId = uri.getPathSegments().get(1);
//                deleteRows = db.delete("Category", "id = ?", new String[]{categoryId});
//                break;
//            default:
//                break;
//        }
//        return deleteRows;
//    }
//
//    @Override
//    public String getType(Uri uri) {
//        String str = "";
//        switch (uriMatcher.match(uri)) {
//            case bookDir:
//                str = "vnd.android.cursor.dir/vnd.com.ziyia.databasetest.provider.Book";
//                break;
//            case bookItem:
//                str = "vnd.android.cursor.item/vnd.com.ziyia.databasetest.provider.Book";
//                break;
//            case categoryDir:
//                str = "vnd.android.cursor.dir/vnd.com.ziyia.databasetest.provider.Category";
//                break;
//            case categoryItem:
//                str = "cnd.android.cursor.item/vnd.com.ziyia.databasetest.provider.Category";
//                break;
//        }
//        return str;
//    }
//
//    @Override
//    public Uri insert(Uri uri, ContentValues values) {
//        SQLiteDatabase db = myDatabaseHelper.getWritableDatabase();
//        Uri uriReturn = null;
//        switch (uriMatcher.match(uri)) {
//            case bookDir:
//            case bookItem:
//                long newBookId = db.insert("Book", null, values);
//                uriReturn = Uri.parse("content://" + AUTHORITY + "/Book/" + newBookId);
//                break;
//            case categoryDir:
//            case categoryItem:
//                long newCategoryId = db.insert("Category", null, values);
//                uriReturn = Uri.parse("content://" + AUTHORITY + "/Category/" + newCategoryId);
//                break;
//            default:
//                break;
//        }
//        return uriReturn;
//    }
//
//    @Override
//    public boolean onCreate() {
//        // TODO: Implement this to initialize your content provider on startup.
//        myDatabaseHelper = new MyDatabaseHelper(getContext(), "BookStrore.db", null, 10);
//        return true;
//    }
//
//    @Override
//    public Cursor query(Uri uri, String[] projection, String selection,
//                        String[] selectionArgs, String sortOrder) {
//        SQLiteDatabase db = myDatabaseHelper.getReadableDatabase();
//        Cursor cursor = null;
//        switch (uriMatcher.match(uri)) {
//            case bookDir:
//                cursor = db.query("Book", projection, selection, selectionArgs, null, null, sortOrder);
//                break;
//            case bookItem:
//                String bookId = uri.getPathSegments().get(1);
//                cursor = db.query("Book", projection, "id = ?", new String[]{bookId}, null, null, sortOrder);
//                break;
//            case categoryDir:
//                cursor = db.query("Category", projection, selection, selectionArgs, null, null, sortOrder);
//                break;
//            case categoryItem:
//                String id = uri.getPathSegments().get(1);
//                cursor = db.query("Category", projection, "id = ?", new String[]{id}, null, null, sortOrder);
//                break;
//            default:
//                break;
//        }
//        return cursor;
//    }
//
//    @Override
//    public int update(Uri uri, ContentValues values, String selection,
//                      String[] selectionArgs) {
//        SQLiteDatabase db = myDatabaseHelper.getWritableDatabase();
//        int updateRows = 0;
//        switch (uriMatcher.match(uri)) {
//            case bookDir:
//                updateRows = db.update("Book", values, selection, selectionArgs);
//                break;
//            case bookItem:
//                String bookId = uri.getPathSegments().get(1);
//                updateRows = db.update("Book", values, "id = ?", new String[]{bookId});
//                break;
//            case categoryDir:
//                updateRows = db.update("Category", values, selection, selectionArgs);
//                break;
//            case categoryItem:
//                String categoryId = uri.getPathSegments().get(1);
//                updateRows = db.update("Category", values, "id = ?", new String[]{categoryId});
//                break;
//            default:
//                break;
//        }
//        return updateRows;
//    }
//}

public class DatabaseProvider extends ContentProvider {
    public static final int BOOK_DIR = 0;
    public static final int BOOK_ITEM = 1;
    public static final int CATEGORY_DIR = 2;
    public static final int CATEGORY_ITEM = 3;
    public static final String AUTHORITY = "com.ziyia.databasetest.provider";
    private static UriMatcher uriMatcher;
    private MyDatabaseHelper dbHelper;
    static {
        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        uriMatcher.addURI(AUTHORITY, "book", BOOK_DIR);
        uriMatcher.addURI(AUTHORITY, "book/#", BOOK_ITEM);
        uriMatcher.addURI(AUTHORITY, "category", CATEGORY_DIR);
        uriMatcher.addURI(AUTHORITY, "category/#", CATEGORY_ITEM);
    }
    @Override
    public boolean onCreate() {
        dbHelper = new MyDatabaseHelper(getContext(), "BookStore.db", null, 2);
        return true;
    }
    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
// 查询数据
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = null;
        switch (uriMatcher.match(uri)) {
            case BOOK_DIR:
                cursor = db.query("Book", projection, selection, selectionArgs,
                        null, null, sortOrder);
                break;
            case BOOK_ITEM:
                String bookId = uri.getPathSegments().get(1);
                cursor = db.query("Book", projection, "id = ?", new String[]
                        { bookId }, null, null, sortOrder);
                break;
            case CATEGORY_DIR:
                cursor = db.query("Category", projection, selection,
                        selectionArgs, null, null, sortOrder);
                break;
            case CATEGORY_ITEM:
                String categoryId = uri.getPathSegments().get(1);
                cursor = db.query("Category", projection, "id = ?", new String[]
                        { categoryId }, null, null, sortOrder);
                break;
            default:
                break;
        }
        return cursor;
    }
    @Override
    public Uri insert(Uri uri, ContentValues values) {
// 添加数据
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Uri uriReturn = null;
        switch (uriMatcher.match(uri)) {
            case BOOK_DIR:
            case BOOK_ITEM:
                long newBookId = db.insert("Book", null, values);
                uriReturn = Uri.parse("content://" + AUTHORITY + "/book/" +
                        newBookId);
                break;
            case CATEGORY_DIR:
            case CATEGORY_ITEM:
                long newCategoryId = db.insert("Category", null, values);
                uriReturn = Uri.parse("content://" + AUTHORITY + "/category/" +
                        newCategoryId);
                break;
            default:
                break;
        }
        return uriReturn;
    }
    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
// 更新数据
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        int updatedRows = 0;
        switch (uriMatcher.match(uri)) {
            case BOOK_DIR:
                updatedRows = db.update("Book", values, selection, selectionArgs);
                break;
            case BOOK_ITEM:
                String bookId = uri.getPathSegments().get(1);
                updatedRows = db.update("Book", values, "id = ?", new String[]
                        { bookId });
                break;
            case CATEGORY_DIR:
                updatedRows = db.update("Category", values, selection,
                        selectionArgs);
                break;
            case CATEGORY_ITEM:
                String categoryId = uri.getPathSegments().get(1);
                updatedRows = db.update("Category", values, "id = ?", new String[]
                        { categoryId });
                break;
            default:
                break;
        }
        return updatedRows;
    }
    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
// 删除数据
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        int deletedRows = 0;
        switch (uriMatcher.match(uri)) {
            case BOOK_DIR:
                deletedRows = db.delete("Book", selection, selectionArgs);
                break;
            case BOOK_ITEM:
                String bookId = uri.getPathSegments().get(1);
                deletedRows = db.delete("Book", "id = ?", new String[] { bookId });
                break;
            case CATEGORY_DIR:
                deletedRows = db.delete("Category", selection, selectionArgs);
                break;
            case CATEGORY_ITEM:
                String categoryId = uri.getPathSegments().get(1);
                deletedRows = db.delete("Category", "id = ?", new String[]
                        { categoryId });
                break;
            default:
                break;
        }
        return deletedRows;
    }
    @Override
    public String getType(Uri uri) {

        switch (uriMatcher.match(uri)) {
            case BOOK_DIR:
                return "vnd.android.cursor.dir/vnd.com.ziyia.databasetest.provider.book";
            case BOOK_ITEM:
                return "vnd.android.cursor.item/vnd.com.ziyia.databasetest.provider.book";
            case CATEGORY_DIR:
                return "vnd.android.cursor.dir/vnd.com.ziyia.databasetest.provider.category";
            case CATEGORY_ITEM:
                return "vnd.android.cursor.item/vnd.com.ziyia.databasetest.provider.category";
        }
        return null;
    }
}